package com.qfmy.app.service.impl.novel;

import com.qfmy.app.mapper.novel.CategoryMapper;
import com.qfmy.app.service.novel.CategoryService;
import com.qfmy.app.vo.novel.CategoryVo;
import com.qfmy.model.entity.novel.Category;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 清风明月
 * @Package com.qfmy.app.service.impl.novel
 * @date 2025/4/14
 * @description 小说分类
 */
@Service
@SuppressWarnings("all")
public class CategoryServiceImpl  implements CategoryService {
    /**
     * 注入mapper
     */
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据类型获取分类
     * @param type
     * @return
     */
    @Override
    public List<CategoryVo> getCategoryByType(Integer type) {
        //根据类型获取分类
        List<Category> categories = categoryMapper.getCategoryByType(type);
        //进行数据拷贝
        List<CategoryVo> categoryVos = CopyCategroyory(categories);
        return categoryVos;
    }

    /**
     * 数据拷贝
     * @param categories
     * @return
     */
    @NotNull
    @Contract("null -> new")
    private List<CategoryVo> CopyCategroyory(List<Category> categories) {
        //判断是否有数据
        if (categories == null || categories.size() == 0) {
            return new ArrayList<>();
        }
        //创建集合
        List<CategoryVo> categoryVos = new ArrayList<>();
        //遍历
        for (Category category : categories) {
            //拷贝数据
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setName(category.getName());
            categoryVo.setId(category.getId());
            categoryVo.setImgUrl(category.getImgUrl());
            categoryVo.setCount(category.getBookCount());
            categoryVos.add(categoryVo);
        }
        return categoryVos;
    }
}
